//
//  JQFYMusicListCell.m
//  JQFY
//
//  Created by 代辉 on 2017/9/4.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYMusicListCell.h"
#import "JQFYMusicPlayer.h"

@interface JQFYMusicListCell ()

@property (nonatomic,weak) UIButton *playButton;
@property (nonatomic,weak) UILabel *musicNameLabel;
@property (nonatomic,weak) UILabel *musicSinger;
@property (nonatomic,strong) JQFYSong *currentModel;
@property (nonatomic,strong) UIImageView *coverImgView;

@end

@implementation JQFYMusicListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.coverImgView = [UIImageView new];
    self.coverImgView.layer.cornerRadius = 5 *SCAL;
    self.coverImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.coverImgView];
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10 *SCAL);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(44 *SCAL);
    }];
    UILabel *musicNameLabel = [UILabel new];
//    musicNameLabel.textColor = [UIColor whiteColor];
    musicNameLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:musicNameLabel];
    [musicNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.coverImgView.mas_right).offset(14 *SCAL);
        make.top.equalTo(self.coverImgView).offset(3 *SCAL);
        make.width.mas_equalTo(200 *SCAL);
    }];
    _musicNameLabel = musicNameLabel;
    
    UILabel *musicSinger = [UILabel new];
    musicSinger.text = @"CharliePuth";
    musicSinger.font = [UIFont systemFontOfSize:16];
    musicSinger.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:musicSinger];
    [musicSinger mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(musicNameLabel);
        make.bottom.equalTo(self.coverImgView).offset(-3 *SCAL);
        make.width.mas_equalTo(200 *SCAL);
    }];
    _musicSinger = musicSinger;
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:playButton];
    [playButton setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateSelected];
    [playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-20 *SCAL);
    }];
    // 热度
    UIImageView *rating = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"Rating%zd",arc4random_uniform((uint32_t)4)+1]]];
    [self.contentView addSubview:rating];
    [rating mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(playButton.mas_left).offset(-10 *SCAL);
        make.centerY.equalTo(playButton);
    }];
    UIImageView *bottomLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
        make.left.right.equalTo(self.contentView);
    }];
    UIImageView *topLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
    [self.contentView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.right.equalTo(self.contentView);
    }];
    
    _playButton = playButton;
    [playButton addTarget:self action:@selector(playOrPause:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)playOrPause:(UIButton *)button
{
//    if (__Player__.songs.count == 0) {
//        return;
//    }
//    if (__Player__.currentModel) {
//        __Player__.currentModel.play = NO;
//        __Player__.currentBtn.selected = NO;
//    }
//    if (__Player__.currentEffView) {
//        __Player__.currentEffView.alpha = 0.7;
//        __Player__.currentEffView = nil;
//    }
    if (button.selected) {
        button.selected = NO;
        [__Player__ pause];
        return;
    }
    
    button.selected = !button.selected;
//    self.model.play = !self.model.play;
//    __Player__.currentBtn = button;
//    __Player__.currentModel = self.model;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"recommendMusicPlay" object:self.cellIndex];
}

- (void)setModel:(JQFYSong *)model
{
    _model = model;
//    if (__Player__.playIndexPath.section == 1 && __Player__.isPlaying) {
//        if (__Player__.songs[__Player__.playIndexPath.row] == model) {
//            model.play = YES;
//        } else {
//            model.play = NO;
//        }
//    }
    _playButton.selected = model.play;
    _musicSinger.text = model.music_auth == nil ? @"Artist Title - Music Categroy" : model.music_auth;
    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:model.music_cover_path] placeholderImage:[UIImage imageNamed:@"2（合并）"]];
    _musicNameLabel.text = model.music_name == nil ? @"UNINHIBITED" : [NSString stringWithFormat:@"%@",model.music_name];
}

@end
